<template>
  <div class="search">
    <Card>
      <Row @keydown.enter.native="handleSearch">
        <Form
          ref="searchForm"
          :model="searchForm"
          inline
          :label-width="70"
          @submit.native.prevent
          class="search-form"
        >
          <Form-item label="手机号" prop="mobile">
            <Input
              type="text"
              v-model="searchForm.mobile"
              placeholder="请输入手机号"
              :maxlength="11"
              clearable
              style="width: 200px"
            />
          </Form-item>
          <Form-item label="审核状态" prop="status">
            <Select
              v-model="searchForm.status"
              placeholder="请选择审核状态"
              clearable
              style="width: 200px"
            >
              <Option
                v-for="(item, index) in statusList"
                :key="index"
                :value="item.value"
                >{{ item.label }}</Option
              >
            </Select>
          </Form-item>
          <Button
            @click="handleSearch"
            class="search-btn"
            type="primary"
            icon="ios-search"
            >搜索</Button
          >
        </Form>
      </Row>
      <Row class="operation padding-row">
        <Button type="primary" @click="handleSetType">设置认证类型</Button>
      </Row>
      <Table
        :loading="loading"
        border
        :columns="columns"
        sortable="custom"
        class="mt_10"
        :data="data"
        ref="table"
      ></Table>
      <Row type="flex" justify="end" class="mt_10">
        <Page
          :current="searchForm.page"
          :total="total"
          :page-size="searchForm.pageSize"
          @on-change="changePage"
          @on-page-size-change="changePageSize"
          :page-size-opts="[10, 20, 50]"
          size="small"
          show-total
          show-elevator
          show-sizer
        />
      </Row>
    </Card>
    <Modal
      v-model="detailShow"
      :title="detailAction"
      :width="900"
      :footer-hide="detailAction == '详情'"
    >
      <div v-if="detailRow" class="modal-info">
        <p>申请信息</p>
        <div class="modal-row">
          <div>审核状态：</div>
          <div>
            {{
              detailRow.status == 1
                ? "已通过"
                : detailRow.status == 2
                ? "不通过"
                : "待审核"
            }}
          </div>
        </div>
        <div class="modal-row">
          <div>姓名：</div>
          <div>{{ detailRow.realname }}</div>
        </div>
        <div class="modal-row">
          <div>手机号：</div>
          <div>{{ detailRow.mobile }}</div>
        </div>
        <div class="modal-row">
          <div>身份证：</div>
          <div>{{ detailRow.identity }}</div>
        </div>
        <div class="modal-row">
          <div>认证类型：</div>
          <div>{{ formatType(detailRow.type) }}</div>
        </div>
        <div class="modal-row">
          <div>认证说明：</div>
          <div>{{ detailRow.content }}</div>
        </div>
        <div class="modal-row">
          <div>认证材料：</div>
          <div>
            <img v-for="(i, k) in detailRow.imgList" :key="k" :src="i" />
          </div>
        </div>
      </div>
      <div slot="footer">
        <Button type="success" @click="submitForm(true)">通过</Button>
        <Button type="primary" @click="submitForm(false)">拒绝</Button>
      </div>
    </Modal>
    <Modal v-model="refuseShow" title="审核拒绝">
      <div>
        <Input
          type="textarea"
          v-model="remarks"
          :autosize="{ minRows: 4, maxRows: 4 }"
          placeholder="请输入拒绝原因"
          :maxlength="100"
          show-word-limit 
        />
      </div>
      <div slot="footer">
        <Button type="text" @click="refuseShow = false">关闭</Button>
        <Button type="primary" @click="approved(2)">提交</Button>
      </div>
    </Modal>
    <Modal v-model="typeShow" title="设置认证类型" footer-hide>
      <div>
        <div class="columnItem" v-for="i in typeList" :key="i.id">
          <span>{{ i.title }} </span><Icon type="md-close" @click="handleDelType(i)" />
        </div>
        <div class="columnItem" @click="handleShow">
          <Icon type="md-add" />
        </div>
        <Input v-if="inputShow" v-model="typeInput" :maxlength="10" @keyup.enter.native="handleAdd">
          <span @click="handleAdd" slot="append">添加</span>
        </Input>
      </div>
    </Modal>
  </div>
</template>

<script>
import * as API_community from "@/api/community.js";

export default {
  name: "authen-list",
  data() {
    return {
      loading: false, // 加载状态
      searchForm: {
        // 请求参数
        page: 1,
        pageSize: 10,
        mobile: null, // 手机号
        status: null, // 审核状态
      },
      statusList: [
        // 审核状态列表
        { value: 0, label: "待审核" },
        { value: 1, label: "通过" },
        { value: 2, label: "不通过" },
      ],
      columns: [
        // 表头
        { title: "序号", minWidth: 100, type: "index" },
        { title: "姓名", key: "realname", minWidth: 120, tooltip: true },
        { title: "身份证号", key: "identity", minWidth: 180, tooltip: true },
        { title: "手机号", key: "mobile", minWidth: 120, tooltip: true },
        {
          title: "认证类型",
          minWidth: 120,
          tooltip: true,
          render: (h, params) => {
            return h("span", {}, this.formatType(params.row.type));
          },
        },
        { title: "认证说明", key: "content", minWidth: 180, tooltip: true },
        {
          title: "申请时间",
          minWidth: 180,
          tooltip: true,
          render: (h, params) => {
            return h("span", {}, this.format(params.row.ctime));
          },
        },
        {
          title: "审核状态",
          minWidth: 100,
          render: (h, params) => {
            let span = h("span", { style: { color: "#FA6419" } }, "待审核");
            if (params.row.status == 1) span = h("span", {}, "通过");
            if (params.row.status == 2) span = h("span", {}, "不通过");
            return span;
          },
        },
        {
          title: "操作",
          align: "center",
          width: 200,
          fixed: "right",
          render: (h, params) => {
            let func = [];
            if (params.row.status == 0) {
              func.push(this.btnDom(h, "primary", "审核", "handleView", params));
            } else {
              func.push(this.btnDom(h, "primary", "详情", "handleView", params, true));
            }
            return h(
              "div",
              {
                style: { display: "flex", justifyContent: "center" },
              },
              func
            );
          },
        },
      ],
      data: [], // 表单数据
      total: 0, // 表单数据总数
      detailRow: {}, // 当前操作行
      detailShow: false, // 审核、详情弹窗
      detailAction: "", // 弹窗标题
      refuseShow: false, // 审核拒绝弹窗
      remarks: "", // 拒绝原因
      typeShow: false, // 认证类型弹窗
      typeList: [], // 认证类型列表
      inputShow: false, // 认证类型弹窗
      typeInput: "", //认证类型输入框
    };
  },
  methods: {
    // 分页 改变页码
    changePage(v) {
      this.searchForm.page = v;
      this.getData();
    },
    // 分页 改变页数
    changePageSize(v) {
      this.searchForm.pageSize = v;
      this.searchForm.page = 1;
      this.getData();
    },
    // 搜索
    handleSearch() {
      this.searchForm.page = 1;
      this.getData();
    },
    // 时间戳转换
    format(v) {
      let date = "";
      if (v) date = new Date(v * 1000).Format("yyyy-MM-dd hh:mm:ss");
      return date;
    },
    // 转换类型
    formatType(v) {
      let label = "";
      this.typeList.forEach((i) => {
        if (i.id == v) label = i.title;
      });
      return label;
    },
    // 按钮
    btnDom(h, type = null, label = "", method, params, ghost = false) {
      return h(
        "Button",
        {
          props: { type, size: "small", ghost },
          style: { marginRight: "5px" },
          on: {
            click: () => {
              this[method](params.row, label);
            },
          },
        },
        label
      );
    },
    // 设置认证类型
    handleSetType() {
      this.typeShow = true;
    },
    // 打开认证类型
    handleShow() {
      this.typeInput = "";
      this.inputShow = !this.inputShow;
    },
    // 添加认证类型
    handleAdd() {
      API_community.authenTypeAdd({ title: this.typeInput }).then((res) => {
        if (res.code == 200) {
          this.$Message.success(res.msg);
          this.inputShow = false;
          this.getTypeList();
        }
      });
    },
    // 删除认证类型
    handleDelType(v) {
      this.inputShow = false;
      this.$Modal.confirm({
        title: "确认删除",
        content: "<p>您确认要删除该认证类型？</p>",
        onOk: () => {
          API_community.authenTypeDel({ id: v.id }).then((res) => {
            if (res.code == 200) {
              this.$Message.success(res.msg);
              this.getTypeList();
            }
          });
        },
      });
    },
    // 详情
    handleView(v, label) {
      this.detailRow = Object.assign({}, v);
      this.detailAction = label;
      this.detailShow = true;
    },
    // 提交审核
    submitForm(v) {
      if (v) {
        // 通过
        this.approved(1);
      } else {
        // 拒绝
        this.detailShow = false;
        this.remarks = "";
        this.refuseShow = true;
      }
    },
    // 审核
    approved(status) {
      let params = {
        id: this.detailRow.id,
        remake: status == 1 ? "" : this.remarks,
        status,
      };
      API_community.authenAudit(params).then((res) => {
        if (res.code == 200) {
          this.$Message.success(res.msg);
          this.detailRow = {};
          this.detailShow = false;
          this.refuseShow = false;
          this.getData();
        }
      });
    },
    // 获取认证类型列表
    getTypeList() {
      API_community.authenTypelist()
        .then((res) => {
          if (res.code == 200) {
            this.typeList = res.typelist;
          }
        })
        .catch(() => {});
    },
    // 查询认证列表
    getData() {
      this.loading = true;
      API_community.authenList(this.searchForm)
        .then((res) => {
          if (res.code == 200) {
            this.data = res.data;
            this.data.forEach((i) => {
              i.imgList = [];
              if (i.thumbs) i.imgList = i.thumbs.split(",");
            });
            this.total = res.total;
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
  },
  mounted() {
    this.getTypeList();
    this.getData();
  },
};
</script>
<style lang="scss" scoped>
::v-deep .ivu-table-wrapper {
  width: 100%;
}
::v-deep .ivu-card {
  width: 100%;
}
.modal-info {
  p::before {
    display: inline-block;
    content: "";
    width: 3px;
    height: 14px;
    vertical-align: top;
    border-radius: 2px;
    margin-right: 20px;
    background-color: #1989fa;
  }
  .modal-row {
    display: flex;
    line-height: 35px;
    padding: 0 20px;
    > div:first-child {
      width: 90px;
      text-align: right;
      padding-right: 5px;
      color: #777777;
    }
    > div:last-child {
      flex: 1;
      word-break: break-all;
      img {
        height: 150px;
        min-width: 150px;
        margin: 0 20px 20px 0;
        border-radius: 8px;
      }
    }
  }
}

.columnItem {
  user-select: none;
  display: inline-block;
  padding: 6px 15px;
  border-radius: 2px;
  color: #ff605c;
  border: 1px solid transparent;
  white-space: nowrap;
  background-color: #ffe5e5;
  margin: 10px 10px 10px 0;
  &:hover {
    cursor: pointer;
    border: 1px solid #ff605c;
  }
  span {
    margin-right: 10px;
  }
}
</style>
